package com.ideasmaker.dao.user.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.ideasmaker.bean.User;
import com.ideasmaker.common.HibernateTemplateSupport;
import com.ideasmaker.common.PageArg;
import com.ideasmaker.dao.user.IUserDao;
import com.ideasmaker.service.user.impl.UserQueryParam;
import com.ideasmaker.util.HibernateSessionUtil;
import com.ideasmaker.util.StrUtil;
/**
 * 
 * @author arlord
 *
 */
@Repository
public class UserDaoImpl extends HibernateTemplateSupport implements IUserDao {
	
	private Logger logger = Logger.getLogger(UserDaoImpl.class);
	
	public void saveUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		logger.info("==========userDaoImpl saveUser ==========");
		this.getHibernateTemplate().save(user);

	}

	public void deleteUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		logger.info("========== userDaoImpl deleteUser ========");
		this.getHibernateTemplate().delete(user);

	}

	public void updateUser(User user) throws SQLException {
		// TODO Auto-generated method stub
		logger.info("========= userDaoImpl updateUser =========");
		this.getHibernateTemplate().update(user);

	}

	public User queryUserById(User user) throws SQLException {
		// TODO Auto-generated method stub
		logger.info("========= userDaoImpl queryUserById========");
		Session session = HibernateSessionUtil.getSession();
		Criteria criteria = session.createCriteria(User.class)
				.add(Restrictions.eq("id", user.getId()));
		return (User)criteria.list().get(0);
	}

	public List<User> queryUserByFilter(UserQueryParam param) throws SQLException {
		// TODO Auto-generated method stub
		logger.info("======== userDaoImpl queryUserByFilter =======");
		User user = param.getUser();
		PageArg pageArg = param.getPageArg();
		Session session = HibernateSessionUtil.getSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.like("name", user.getName(), MatchMode.ANYWHERE));
		
		if(!StrUtil.isNullStr(user.getGender())){
			
			criteria.add(Restrictions.eq("gender", user.getGender()));
		}
		if(!StrUtil.isNullStr(user.getLabel())){
			
			criteria.add(Restrictions.like("label", user.getLabel(),MatchMode.ANYWHERE));
		}
		criteria.setFirstResult(pageArg.getStartResult());
		criteria.setMaxResults(pageArg.getPageSize());
		@SuppressWarnings("unchecked")
		List<User> list = criteria.list();
		return list;
	}

	public int queryRecordCountByFilter(User user) throws SQLException {
		// TODO Auto-generated method stub

		
		return 0;
	}

	public boolean queryUserByName(User user) throws SQLException {

		HibernateTemplate  hTemplate = this.getHibernateTemplate();
		Session session = hTemplate.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("name", user.getName()));
		List list = criteria.list();
		if(list != null && list.size() > 0){
			return true;
		}
		return false;
	}

}
